package com.ruoyi.project.api.config;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Component
public class ALIMessageUtil {

    private  final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ALIMessageUtil.class);
    public static final String ALIBABA_CLOUD_ACCESS_KEY_ID = "LTAI5tPLyy4MzvoRBAXzdUYh";
    public static final String ALIBABA_CLOUD_ACCESS_KEY_SECRET = "AAqqpZEHc0BRfgGmzWtMeXn1FoOzMa";
    public static final String ALIBABA_CLOUD_REGION_ID = "cn-qingdao";
    public static final String ALIBABA_CLOUD_ENDPOINT = "dysmsapi.aliyuncs.com";
    public static final String ALIBABA_CLOUD_SIGNNAME = "郑州诚德网络科技";
    public static final String ALIBABA_CLOUD_TEMPLATE_CODE = "SMS_298515167";

    public static boolean sendMessage(String phone, Map<String, Object> params) {
        // 阿里云账号凭证配置
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(ALIBABA_CLOUD_ACCESS_KEY_ID)
                .accessKeySecret(ALIBABA_CLOUD_ACCESS_KEY_SECRET)
                .build());

        // 异步客户端
        AsyncClient client = AsyncClient.builder()
                .region(ALIBABA_CLOUD_REGION_ID)                               // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride(ALIBABA_CLOUD_ENDPOINT)
                )
                .build();

        // 发送API参数配置
        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .signName(ALIBABA_CLOUD_SIGNNAME)                   //签名
                .templateCode(ALIBABA_CLOUD_TEMPLATE_CODE)          //模板CODE
                .phoneNumbers(phone)                                //接收短信的手机号码
                .templateParam(JSONObject.toJSONString(params))     //短信内容
                .build();

        // 发送短信并且获得返回结果
        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);

        try {
            SendSmsResponse resp = response.get();
            System.out.println(new Gson().toJson(resp.getBody()));
            return "OK".equals(resp.getBody().getCode());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭客户端
            client.close();
        }
        return false;
    }


    public static void main(String[] args) {

        String Phone = "13137709629";
            //随机生成六位数字验证码
            String code = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
            Map<String, Object> params = new HashMap<>();
            params.put("code", code);
            boolean isOk = ALIMessageUtil.sendMessage(Phone, params);
            log.info(Phone+"-已发送短信：" + "验证码为： "+ code);
            log.info(Phone+"-已发送短信：" + "isOk： "+ isOk);
            //将验证码存在Redis中，并且5分钟内有效

    }

}